/**
 * @author uangbo
 * TODO// N/A
 */
package com.platform.manager.action;

import java.util.Date;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.platform.bean.User;
import com.platform.manager.bo.LoginBo;
import com.platform.pojo.UserInfo;

public class LoginAction extends ManagerSupport {

	private User user;	//用户信息
	private String msg;	//返回信息
	private boolean success;	//是否验证成功
	
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public String execute() throws Exception {
		final LoginBo loginBo = new LoginBo();
		final UserInfo loginUser = loginBo.queryUser(user.getUsername(), user.getPassword());
		//返回
		if(loginUser != null && loginUser.getIsAudit().intValue() == 1) {
			success = true;
			msg = "登录成功！";
			//将用户信息放入session
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("user", loginUser);
			//将最新的用户信息更新到数据库中
			loginUser.setLastLoginTime(new Date());
			//获得客户端IP地址
			String ip = ServletActionContext.getRequest().getRemoteAddr();
			loginUser.setLastLoginIp(ip);
			new Thread() {
				public void run() {
					loginBo.updateUserInfo(loginUser);
				}
			}.start();
		} else if(loginUser != null && loginUser.getIsAudit().intValue() != 1) {
			success = false;
			msg = "帐号锁定中，请联系管理员解锁！";
		} else {
			success = false;
			msg = "用户名或密码错误！";
		}
		return SUCCESS;
	}

}
